<template>
  <el-dialog :visible.sync="dialogVisible" title="预约试听课" width="600px" @open="openDialog" @close="closeDialog">
    <el-form ref="form" :model="form" :rules="ruleForm" label-width="120px">
      <el-form-item label="试听科目" prop="courseName">
        <el-input v-model="form.courseName" placeholder="请输入试听科目" auto-complete="off" />
      </el-form-item>
      <el-form-item label="试听课时间" prop="listenDate">
        <el-date-picker v-model="form.listenDate" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" type="datetime"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button :loading="dialogLoading" type="primary" @click="submitListen">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { submitCustomerListen, getCustomerListen } from '@/api/customer'

export default {
  name: 'ListenDialog',
  // eslint-disable-next-line vue/require-prop-types
  props: ['visible', 'listenId', 'opportunityId'],
  data() {
    return {
      dialogLoading: false,
      dialogVisible: this.visible,
      form: {
        opportunityId: null,
        listenId: null,
        courseName: null,
        listenDate: ''
      },
      ruleForm: {
        courseName: [{ required: true, message: '请输入试听课科目', trigger: 'blur' }],
        listenDate: [{ required: true, message: '请选择试听时间', trigger: 'blur' }]
      },
      searchTeacherLoading: false
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
    }
  },
  created() {

  },
  methods: {
    clearData() {
      this.form.opportunityId = null
      this.form.listenId = null
      this.form.courseName = ''
      this.form.listenDate = ''
    },
    openDialog() {
      this.clearData()
      if (this.listenId) {
        this.form.listenId = this.listenId
        getCustomerListen(this.listenId).then(res => {
          this.form = res.data
        })
      }
    },
    closeDialog() {
      this.$emit('close-dialog', false)
    },
    submitListen() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.opportunityId = this.opportunityId
          this.dialogLoading = true
          submitCustomerListen(this.form).then(() => {
            this.$message('提交试听课成功')
            this.$emit('close-dialog', true)
          }).finally(() => {
            this.dialogLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
